package Jvm;

import org.apache.ecs.xhtml.object;

/**
 * Created by kai on 2015/8/17 0017.
 * 这里面逻辑:
 * a,b,c a调用notifyall以后 a,b,c会同时加入竞争.所以存在这种情况: a释放了锁以后,
 * 还是a抢到了锁.但是这里面有判断.如果a不能执行了 ,会让他进入wait,所以能保证代码顺序执行
 */
public class WaitNotifyTest {
    private static Object lock = new Object();
    private static String threadName = "A";

    public static void main(String[] args) {

        new Thread(new myThread()).start();
        new Thread(new myThread2()).start();
        new Thread(new myThread3()).start();
    }


    static class myThread extends Thread {

        private int count = 0;

        @Override
        public void run() {
            while (count < 10) {
                synchronized (lock) {
                    System.out.println("i am a");
                    if (threadName.equals("A")) {
                        System.out.println("curr thread is :" + threadName);
                        threadName = "B";
                        count++;
                        lock.notifyAll();
                    } else {
                        try {
                            System.out.println("i will wait. a..");
                            lock.wait();
//                        Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }

    static class myThread2 extends Thread {
        private int count = 0;

        @Override
        public void run() {
            while (count < 10) {
                synchronized (lock) {
                    System.out.println("i am b");
                    if (threadName.equals("B")) {
                        System.out.println("curr thread is :" + threadName);
                        threadName = "C";
                        count++;
                        lock.notifyAll();
                    } else {
                        try {
                            lock.wait();
                            Thread.sleep(3000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }

    static class myThread3 extends Thread {
        private int count = 0;

        @Override
        public void run() {
            while (count < 10) {


                synchronized (lock) {
                    System.out.println("i am c");
                    if (threadName.equals("C")) {
                        System.out.println("curr thread is :" + threadName);
                        threadName = "A";
                        lock.notifyAll();
                        count++;
                    } else {
                        try {
                            lock.wait();
                            Thread.sleep(3000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}
